﻿using Microsoft.Office.Interop.Excel;
using MiniExcelLibs;
using OfficeOpenXml.FormulaParsing.Excel.Functions.RefAndLookup;
using PanhuaWin.系统管理;
using PanhuaWin.通用;
using StackExchange.Redis;
using Sunny.UI;
using Sunny.UI.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.UI;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using DataTable = System.Data.DataTable;

namespace PanhuaWin.车间
{
    public partial class Form硅钢计划维护 : UIPage
    {
        string 工厂编号1;
        string 工厂guid1;
        string 部门编号1;
        string FormGuid;
        int 开始行 = -1;
        int 结束行 = -1;
        string 部门guid;
        string FormWorkType = "硅钢";

        public string 产线编号 { get; private set; }
        public string 产线名称 { get; private set; }

        public Form硅钢计划维护()
        {
            InitializeComponent();
        }

        private void splitContainer3_Panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void Form硅钢计划维护_Load(object sender, EventArgs e)
        {
            this.uiDatePicker1.Value = DateTime.Today.AddMonths(-1);
            this.uiDatePicker2.Value = DateTime.Today;
            this.uiDatePicker计划日期.Value = DateTime.Today;
            pub.ListWorkShop(pub.sys_user, this.uiComboBoxWorkShop);
            pub.ListSteelAll(this.uiComboBox材质1);
            FormGuid = Guid.NewGuid().ToString();
            DeleteTempPlan();
        }

        private void DeleteTempPlan()
        {
            string sql;
            sql = "delete Temp_硅钢计划 where mac='" + pub.getMac() + "'";
            pub.ExecuteSql(pub.ConnectionString, sql, "进入界面时删除Temp_硅钢计划");
        }

        private void uiComboBoxWorkShop_SelectedIndexChanged(object sender, EventArgs e)
        {
            string sql;
            工厂编号1 = this.uiComboBoxWorkShop.SelectedValue.ToString();
            sql = @"SELECT   guid, WorkShopCode, WorkShopName, AuditDate, AuditStatus, AuditId, Auditor, Remark, CreateID, Creator, 
                CreateDate, ModifyID, Modifier, ModifyDate   FROM      Dic_WorkShop     where  WorkShopCode='" + 工厂编号1 + "'      ";
            System.Data.DataTable tb = pub.GetDataTable(pub.ConnectionString, sql);
            if (tb.Rows.Count > 0)
            {
                //                sql = @"SELECT    guid, WorkShopGuid, WarehouseCode, WarehouseName, WarehouseDesc, AuditDate, AuditStatus, AuditId, 
                //                Auditor, Remark, CreateID, Creator, CreateDate, ModifyID, Modifier, ModifyDate
                //FROM      Dic_Warehouse where WorkShopGuid ='" + tb.Rows[0]["guid"].ToString() + "'";
                //                DataTable tb2 = pub.GetDataTable(pub.ConnectionString, sql);
                //pub.ListWareHouse(this.uiComboBox仓库编号1, tb.Rows[0]["guid"].ToString());
                //if (this.uiComboBox仓库编号1.SelectedValue.ToString() != "")
                //{
                //    Do库位刷新1(this.uiComboBox仓库编号1.SelectedValue.ToString());
                //}
                pub.ListWorkLine(this.uiComboBoxWorkShop.SelectedValue.ToString(), "硅钢", this.uiComboBox产线);

            }
        }





        private void uiButton查询_Click(object sender, EventArgs e)
        {
            //显示所有仓库收到的原料
            ListData();
            ListTempPlan();


        }

        private void ListData()
        {
            try
            {
                string sql;
                if (this.uiComboBoxWorkShop.SelectedText.ToString() == "")
                {
                    MessageBox.Show("工厂要选择,请重试!", "提示", MessageBoxButtons.OK);
                    return;
                }
                工厂编号1 = this.uiComboBoxWorkShop.SelectedValue.ToString();
                工厂guid1 = pub.GetWorkShopByCode(工厂编号1).guid.ToString();
                //if (this.uiTextBox部门1.Text.ToString() == "")
                //{
                //    MessageBox.Show("部门要选择,请重试!", "提示", MessageBoxButtons.OK);
                //    return;
                //}
                //部门编号1 = this.uiTextBox部门1.Text;
                //部门guid = pub.GetDepartByCode(部门编号1, 工厂编号1).guid.ToString();
                string WorkShopCode = 工厂编号1;
                //    if (this.uiComboBox仓库编号1.SelectedText=="")
                //{
                //    MessageBox.Show("仓库要选择,请重试!", "提示", MessageBoxButtons.OK);
                //    return;
                //}




                sql = "SELECT    row_number() over(order by 卷号) 序号,   工厂编号, 日期, 卷号, 规格型号, 材质, 产地, 重量, 米数, 状态 FROM View_能被硅钢计划 where 1=1  ";
                sql += " and 工厂编号='" + WorkShopCode + "' and 日期>='" + this.uiDatePicker1.Value.ToString("yyyy-MM-dd") + "' and 日期<'" + this.uiDatePicker2.Value.AddDays(1).ToString("yyyy-MM-dd") + "'";
                sql += " and 卷号 not in ( select VolumeCodeNew from Temp_硅钢计划  ";
                sql += " union all  select OriginalVolumeNew from Bus_SiOriginal   ";
                sql += " union all  select VolumeCodeNew from  Bus_PlanSi  )";


                if (this.uiTextBox卷号.Text != "")
                {
                    sql += " and 卷号 like '%" + this.uiTextBox卷号.Text.Trim().Replace("--", "") + "%'";
                }
                if (this.uiTextBox规格型号1.Text != "")
                {
                    sql += " and 规格型号 like '" + this.uiTextBox规格型号1.Text.Trim().Replace("--", "") + "'";
                }
                if (this.uiComboBox材质1.SelectedText != "全部")
                {
                    sql += " and 材质='" + this.uiComboBox材质1.SelectedText + "'";
                }

                System.Data.DataTable tb = pub.GetDataTable(pub.ConnectionString, sql);
                this.uiDataGridView待选.AutoGenerateColumns = false;
                this.uiDataGridView待选.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
                this.uiDataGridView待选.DataSource = tb;
                this.uiDataGridView待选.Refresh();
            }
            catch
            {

            }
        }

        private void uiButton选择部门1_Click(object sender, EventArgs e)
        {

            Form部门档案 form部门档案 = new Form部门档案();
            form部门档案.工厂编号 = this.uiComboBoxWorkShop.SelectedValue.ToString();
            if (form部门档案.ShowDialog(this) == DialogResult.OK)
            {
                this.uiTextBox部门1.Text = form部门档案.部门编号;
                this.uiTextBox部门名称1.Text = form部门档案.部门名称;
                部门guid = form部门档案.Guid.ToString();
                pub.ListWorkLine(form部门档案.工厂编号, this.uiTextBox部门1.Text, FormWorkType, this.uiTextBox产线搜索1.Text.Trim().Replace("--", ""), this.uiComboBox产线);
            }
        }

        private void uiTextBox产线搜索1_TextChanged(object sender, EventArgs e)
        {
            string workType = "硅钢";
            pub.ListWorkLine(工厂编号1, this.uiTextBox部门1.Text, FormWorkType, this.uiTextBox产线搜索1.Text.Trim().Replace("--", ""), this.uiComboBox产线);
        }

        private void uiLabel4_Click(object sender, EventArgs e)
        {

        }

        private void uiButton导出_Click(object sender, EventArgs e)
        {
            string path;
            this.saveFileDialog1.Filter = "Excel 文件(*.xlsx)|*.xlsx ";

            try
            {
                string sql;
                string WorkShopCode = this.uiComboBoxWorkShop.SelectedValue.ToString();


                sql = "SELECT    row_number() over(order by VolumeCode) 序号, WorkShopCode 工厂编号, CreateDate 入库日期 ,WarehouseCode 仓库编号,  DocCode 入库单号,  StorageLocation 库位编号,  ";
                sql += "  InventoryCode 存货编号, Size 规格型号,Material 材质, Address 产地,   ";
                sql += " VolumeCodeNew 新卷号,  Weight 重量  FROM      View_能被硅钢计划 where 1=1  ";
                sql += " and WorkShopCode='" + WorkShopCode + "'";
                if (this.uiTextBox卷号.Text != "")
                {
                    sql += " and VolumeCodeNew like '%" + this.uiTextBox卷号.Text.Trim().Replace("--", "") + "%'";
                }
                if (this.uiTextBox规格型号1.Text != "")
                {
                    sql += " and Size like '" + this.uiTextBox规格型号1.Text.Trim().Replace("--", "") + "'";
                }
                if (this.uiComboBox材质1.SelectedText != "全部")
                {
                    sql += " and Material='" + this.uiComboBox材质1.SelectedText + "'";
                }

                System.Data.DataTable tb = pub.GetDataTable(pub.ConnectionString, sql);
                if (this.saveFileDialog1.ShowDialog(this) == DialogResult.OK)
                {
                    path = this.saveFileDialog1.FileName;
                    if (!File.Exists(path))
                    {
                        MiniExcel.SaveAs(path, tb, true, "sheet1", ExcelType.XLSX);
                    }
                    else
                    {
                        MessageBox.Show("文件已经存在,请重试!", "提示", MessageBoxButtons.OK);
                        return;
                    }

                }

            }
            catch (Exception ex)
            {

            }

        }

        private void uiDataGridView待选_MouseDown(object sender, MouseEventArgs e)
        {

        }

        private void uiDataGridView待选_MouseUp(object sender, MouseEventArgs e)
        {

        }

        private void uiDataGridView待选_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            开始行 = e.RowIndex;
        }

        private void uiDataGridView待选_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
        {
            结束行 = e.RowIndex;
        }


        private void uiCheckBox选中行打钩_CheckedChanged(object sender, EventArgs e)
        {
            if (开始行 <= 结束行)
            {

            }
            else
            {
                int p = 开始行;
                开始行 = 结束行;
                结束行 = p;
            }
            if (开始行 >= 0 && 结束行 >= 0)
            {
                for (int i = 开始行; i <= 结束行; i++)
                {
                    this.uiDataGridView待选.Rows[i].Cells["C选中"].Value = this.uiCheckBox选中行打钩.Checked;
                }
            }

        }

        private void uiButtont添加选中_Click(object sender, EventArgs e)
        {
            开始行 = -1;
            结束行 = -1;
            string VolumeCodeNew;
            string guid;
            string outPutSize = this.uiTextBox轧制规格.Text;
            string productSize = this.uiTextBox成品规格.Text;

            if (!pub.IsRightSize(outPutSize) && outPutSize != "")
            {
                MessageBox.Show("轧制规格要么空着，要么要填对,请重试!", "提示", MessageBoxButtons.OK);
                return;
            }
            if (!pub.IsRightSize(productSize) && productSize != "")
            {
                MessageBox.Show("成品规格要么空着，要么要填对,请重试!", "提示", MessageBoxButtons.OK);
                return;
            }
            if (this.uiComboBoxWorkShop.SelectedText.ToString() == "")
            {
                MessageBox.Show("工厂要选择,请重试!", "提示", MessageBoxButtons.OK);
                return;
            }
            工厂编号1 = this.uiComboBoxWorkShop.SelectedValue.ToString();
            工厂guid1 = pub.GetWorkShopByCode(工厂编号1).guid.ToString();
            if (this.uiTextBox部门1.Text.ToString() == "")
            {
                MessageBox.Show("部门要选择,请重试!", "提示", MessageBoxButtons.OK);
                return;
            }
            部门编号1 = this.uiTextBox部门1.Text;
            部门guid = pub.GetDepartByCode(部门编号1, 工厂编号1).guid.ToString();
            if (this.uiComboBox产线.SelectedText == "")
            {
                MessageBox.Show("产线要选择,请重试!", "提示", MessageBoxButtons.OK);
                return;
            }
            产线编号 = this.uiComboBox产线.SelectedValue.ToString();
            产线名称 = this.uiComboBox产线.SelectedText;
            if (this.uiComboBox去向.SelectedText == "")
            {
                MessageBox.Show("去向要选择,请重试!", "提示", MessageBoxButtons.OK);
                return;
            }
            string nextWorkType = this.uiComboBox去向.SelectedText;
            string remark = this.uiTextBox备注.Text.Trim().Replace("--", "");
            guid = FormGuid;
            if (this.uiDataGridView待选.Rows.Count > 0)
            {

                for (int j = 0; j < this.uiDataGridView待选.Rows.Count; j++)
                {
                    DataGridViewRow urow = this.uiDataGridView待选.Rows[j];
                    if (urow.Cells["C选中"].Value != null)
                    {
                        if ((bool)urow.Cells["C选中"].Value)
                        {
                            VolumeCodeNew = urow.Cells["C新卷号"].Value.ToString();
                            InsertTemp计划(guid, VolumeCodeNew, nextWorkType, outPutSize, productSize, remark);
                        }
                    }

                }
                //重新刷新
                ListData();
                ListTempPlan();
            }
        }

        private void ListTempPlan()
        {
            string sql = @"SELECT  row_number() over(order by  时间) 序号, WorkShopGuid, WorkShopCode, DepartGuid, DepartCode, WorkLineCode, WorkLineName, WorkDate, RowNumber,
               Originalsize 热轧规格, Address 产地, VolumeCode , VolumeCodeNew 热轧批号, Weight 重量, OutputSize 轧制规格, ProductSize 成品规格,NextWorkType 去向, Material 材质, StorageLocation 库位, MAC,
               时间,remark 备注 FROM Temp_硅钢计划  where guid='" + FormGuid + "'   ";
            System.Data.DataTable tb1 = pub.GetDataTable(pub.ConnectionString, sql);
            if (tb1.Rows.Count > 0)
            {

                this.uiDataGridView已选.AutoGenerateColumns = false;
                this.uiDataGridView已选.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
                this.uiDataGridView已选.DataSource = tb1;
                this.uiDataGridView已选.Refresh();
            }
            else
            {
                this.uiDataGridView已选.DataSource = null;
                this.uiDataGridView已选.Refresh();
            }

        }

        private void InsertTemp计划(string guid, string VolumeCodeNew, string 去向, string 轧制规格, string 成品规格, string remark)
        {
            string sql;
            sql = " select VolumeCodeNew from Temp_硅钢计划 where VolumeCodeNew ='" + VolumeCodeNew + "' ";
            sql += " union all   ";
            sql += " select OriginalVolumeNew from Bus_SiOriginal where OriginalVolumeNew ='" + VolumeCodeNew + "'";//
            sql += " union all   ";
            sql += " select VolumeCodeNew from  Bus_PlanSi where VolumeCodeNew ='" + VolumeCodeNew + "'";
            System.Data.DataTable tb1 = pub.GetDataTable(pub.ConnectionString, sql);
            if (tb1.Rows.Count > 0)
            {
                //说明有计划或者使用了
            }
            else
            {
                sql = " insert into Temp_硅钢计划 (guid, WorkShopGuid, WorkShopCode, DepartGuid, DepartCode, WorkLineCode, WorkLineName, WorkDate, " +
                      " Originalsize, Address,  VolumeCodeNew, Weight, Meter, OutputSize,ProductSize, NextWorkType, Material,  MAC, 时间,remark)";
                sql += " select '" + guid + "','" + 工厂guid1 + "','" + 工厂编号1 + "','" + 部门guid + "','" + 部门编号1 + "','" + 产线编号 + "',";
                sql += " '" + 产线名称 + "','" + this.uiDatePicker计划日期.Value.ToString("yyyy-MM-dd") + "',规格型号 ,产地,  卷号, 重量,米数,";
                sql += " '" + 轧制规格 + "' , '" + 成品规格 + "' , '" + 去向 + "',材质, '" + pub.getMac() + "' ,getdate(),'" + remark + "' ";
                sql += " from View_能被硅钢计划 where 卷号 ='" + VolumeCodeNew + "'";
                pub.ExecuteSql(pub.ConnectionString, sql, "塞入Temp_硅钢计划");

            }

        }

        private void uiButton清空_Click(object sender, EventArgs e)
        {
            string sql;
            sql = "delete Temp_硅钢计划 where mac ='" + pub.getMac() + "'";
            pub.ExecuteSql(pub.ConnectionString, sql, "清空临时硅钢计划表");
            ListData();
            ListTempPlan();
        }


        private void uiComboBox产线_SelectedValueChanged(object sender, EventArgs e)
        {
            string workLineCode;
            if (this.uiComboBox产线.SelectedValue != null)
            {
                workLineCode = this.uiComboBox产线.SelectedValue.ToString();
                pub.ListWorkType(工厂编号1, "硅钢", this.uiComboBox去向);
            }



        }

        private void uiButton取消选中_Click(object sender, EventArgs e)
        {
            if (this.uiDataGridView已选.Rows.Count > 0)
            {
                int k = 1;
                for (int j = 0; j < this.uiDataGridView已选.Rows.Count; j++)
                {
                    DataGridViewRow urow = this.uiDataGridView已选.Rows[j];
                    if (urow.Cells["D选中"].Value != null)
                    {
                        if ((bool)urow.Cells["D选中"].Value)
                        {
                            string guid = FormGuid;
                            string VolumeCodeNew = urow.Cells["D热轧批号"].Value.ToString();
                            DeleteTemp计划(guid, VolumeCodeNew);
                        }
                    }

                }
                //重新刷新
                ListData();
                ListTempPlan();
            }
        }

        private void DeleteTemp计划(string guid, string volumeCodeNew)
        {
            string sql;
            sql = "delete Temp_硅钢计划 where guid='" + guid + "' and volumeCodeNew='" + volumeCodeNew + "'";
            pub.ExecuteSql(pub.ConnectionString, sql, "DeleteTemp计划-硅钢计划");
        }

        private void uiDataGridView已选_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                if (this.uiDataGridView已选.Columns[e.ColumnIndex].Name == "D取消")
                {
                    DataGridViewRow urow = this.uiDataGridView已选.Rows[e.RowIndex];
                    string guid = FormGuid;
                    string VolumeCodeNew = urow.Cells["D热轧批号"].Value.ToString();
                    DeleteTemp计划(guid, VolumeCodeNew);
                    //重新刷新
                    ListData();
                    ListTempPlan();
                }
            }
        }

        private void uiButton导出模板_Click(object sender, EventArgs e)
        {
            string path;
            string sql = @"SELECT  ''序号,  '' 热轧规格, '' 产地, '' 热轧批号, '' 重量, '' 轧制规格, ''去向,'' 材质, '' 成品规格, ''
               备注 FROM Temp_硅钢计划  where 1=2    ";
            saveFileDialog1.Filter = "Excel文件 (*.XLSX)|*.XLSX";
            DataTable tb = pub.GetDataTable(pub.ConnectionString, sql);
            if (this.saveFileDialog1.ShowDialog(this) == DialogResult.OK)
            {
                path = this.saveFileDialog1.FileName;
                if (!File.Exists(path))
                {
                    MiniExcel.SaveAs(path, tb, true, "sheet1", ExcelType.XLSX);
                }
                else
                {
                    MessageBox.Show("文件已经存在,请重试!", "提示", MessageBoxButtons.OK);
                    return;
                }

            }
        }

        private void uiDataGridView已选_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            //更新内容

            if (this.uiDataGridView已选.Rows.Count > 0)
            {

                string sql;
                int j = e.RowIndex;
                //for (int j = 0; j < this.uiDataGridView已选.Rows.Count; j++)
                //{
                DataGridViewRow urow = this.uiDataGridView已选.Rows[j];
                string outPutSize = urow.Cells["D轧制规格"].Value.ToString().Trim().Replace("--", "");
                string productSize = urow.Cells["D成品规格"].Value.ToString().Trim().Replace("--", "");

                if (!pub.IsRightSize(outPutSize) && outPutSize != "")
                {
                    MessageBox.Show("第" + (j + 1).ToString() + "行的轧制规格要么空着，要么要填对,请重试!", "提示", MessageBoxButtons.OK);
                   
                    return;
                }
                else
                {
                   
                }
                if (!pub.IsRightSize(productSize) && productSize != "")
                {
                    MessageBox.Show("第" + (j + 1).ToString() + "行的成品规格要么空着，要么要填对,请重试!", "提示", MessageBoxButtons.OK);
                   
                    return;

                }
                else
                {
                   
                }
                string nextWorkType = urow.Cells["D去向"].Value.ToString().Trim().Replace("--", "");
                if (!pub.IsRightNextWorkType(工厂编号1, FormWorkType, nextWorkType))
                {
                    MessageBox.Show("第" + (j + 1).ToString() + "行的去向有问题,请重试!", "提示", MessageBoxButtons.OK);

                    return;

                }
                string remark = urow.Cells["D备注"].Value.ToString().Trim().Replace("--", "");
                string volumeCodeNew = urow.Cells["D热轧批号"].Value.ToString().Trim().Replace("--", "");
                sql = " update Temp_硅钢计划 set OutputSize='" + outPutSize + "',ProductSize='" + productSize + "',NextWorkType='" + nextWorkType + "' ,Remark='" + remark + "'";
                sql += " where guid='" + FormGuid + "' and VolumeCodeNew='" + volumeCodeNew + "'";
                pub.ExecuteSql(pub.ConnectionString, sql, "Temp_硅钢计划手动更新");
                //}

            }
        }

        private void uiButton保存_Click(object sender, EventArgs e)
        {
            string sql;
            sql = "select guid, WorkShopGuid, WorkShopCode, DepartGuid, DepartCode, WorkLineCode, WorkLineName, WorkDate, ";
            sql += " RowNumber, Originalsize, Address, VolumeCode, VolumeCodeNew, Weight, OutputSize, ProductSize, NextWorkType, Remark, Material,  ";
            sql += "  MAC from Temp_硅钢计划 where guid='" + FormGuid + "' order by RowNumber ";
            DataTable tb = pub.GetDataTable(pub.ConnectionString, sql);

            if (tb.Rows.Count > 0)
            {    //先  检查
                for (int i = 0; i < tb.Rows.Count; i++)
                {
                    string outPutSize = tb.Rows[i]["OutputSize"].ToString().Trim().Replace("--", "");
                    if (!pub.IsRightSize(outPutSize) && outPutSize != "")
                    {
                        MessageBox.Show("第" + (i + 1).ToString() + "行的轧制规格要么空着，要么要填对,请重试!", "提示", MessageBoxButtons.OK);

                        return;
                    }
                    string productSize = tb.Rows[i]["ProductSize"].ToString().Trim().Replace("--", "");
                    if (!pub.IsRightSize(productSize) && productSize != "")
                    {
                        MessageBox.Show("第" + (i + 1).ToString() + "行的轧制规格要么空着，要么要填对,请重试!", "提示", MessageBoxButtons.OK);

                        return;
                    }
                    string nextWorkType = tb.Rows[i]["NextWorkType"].ToString().Trim().Replace("--", "");
                    if (!pub.IsRightNextWorkType(工厂编号1, FormWorkType, nextWorkType))
                    {
                        MessageBox.Show("第" + (i + 1).ToString() + "行的去向有问题,请重试!", "提示", MessageBoxButtons.OK);

                        return;

                    }
                    string VolumeCodeNew = tb.Rows[i]["VolumeCodeNew"].ToString().Trim().Replace("--", "");
                    if (pub.IsInPlan(工厂编号1, FormWorkType, VolumeCodeNew))
                    {
                        MessageBox.Show("第" + (i + 1).ToString() + "行的卷号已经有计划了,请重试!", "提示", MessageBoxButtons.OK);
                        return;
                    }
                    sql = "SELECT    row_number() over(order by 卷号) 序号,   工厂编号, 日期, 卷号, 规格型号, 材质, 产地, 重量, 米数, 状态 FROM View_能被硅钢计划 where 1=1  ";
                    sql += " and 工厂编号='" + 工厂编号1 + "'  ";
                    sql += " and 卷号 not in ( select VolumeCodeNew from Temp_硅钢计划 where guid<>'" + FormGuid + "'  ";   //把自己排除掉
                    sql += " union all  select OriginalVolumeNew from Bus_SiOriginal   ";
                    sql += " union all  select VolumeCodeNew from  Bus_PlanSi  )";
                    DataTable tb2 = pub.GetDataTable(pub.ConnectionString, sql);
                    if (tb2.Rows.Count == 0)
                    {
                        MessageBox.Show("第" + (i + 1).ToString() + "行的卷号没有从上一计划转入或者完工,请重试!", "提示", MessageBoxButtons.OK);
                        return;
                    }
                }
            }
            try
            {
                ///再塞入
                int r1 = 0;
                int r2 = 0;

                sql = " insert into   Bus_PlanSi (guid, WorkShopGuid, WorkShopCode, DepartGuid, DepartCode, WorkLineCode, WorkLineName, WorkDate, ";
                sql += " RowNumber, Originalsize, Address, VolumeCode, VolumeCodeNew, Weight,Meter, OutputSize, ProductSize, NextWorkType, Remark, Material,  ";
                sql += "  MAC, CreateID, Creator, CreateDate)   ";
                sql += " select newid(), WorkShopGuid, WorkShopCode, DepartGuid, DepartCode, WorkLineCode, WorkLineName, WorkDate, ";
                sql += " row_number() over(order by  时间) RowNumber, Originalsize, Address, VolumeCode, VolumeCodeNew, Weight, Meter,OutputSize, ProductSize, NextWorkType, Remark, Material,  ";
                sql += "   MAC,'" + pub.sys_user.User_Id + "' ,'" + pub.sys_user.UserName + "',getdate() from Temp_硅钢计划 where guid='" + FormGuid + "'   ";
                r1 = pub.ExecuteSql(pub.ConnectionString, sql, "从Temp_硅钢计划塞入Bus_PlanPickling");
                ///清理临时表
                sql = "delete Temp_硅钢计划 where mac ='" + pub.getMac() + "' or  guid='" + FormGuid + "' ";
                r2 = pub.ExecuteSql(pub.ConnectionString, sql, "清空临时硅钢计划表");
                if (r1 * r2 != 0)
                {
                    ListData();
                    ListTempPlan();
                    MessageBox.Show("硅钢计划保存成功!", "提示", MessageBoxButtons.OK);
                    return;
                }
                else
                {
                    ListData();
                    ListTempPlan();
                    MessageBox.Show("硅钢计划保存失败!", "提示", MessageBoxButtons.OK);
                    return;
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show("硅钢计划保存失败!异常信息是:" + ex.Message, "提示", MessageBoxButtons.OK);
            }


        }
        private void uiButton导入_Click(object sender, EventArgs e)
        {
            try
            {
                string path;
                //openFileDialog1.InitialDirectory = Application.ExecutablePath;
                //openFileDialog1.Filter = "Excel文件|*.xlsx";
                //openFileDialog1.FilterIndex = 2;
                openFileDialog1.RestoreDirectory = true;

                this.openFileDialog1.Filter = "Excel 文件(*.xlsx)|*.xlsx";
                if (this.openFileDialog1.ShowDialog(this) == DialogResult.OK)
                {
                    path = this.openFileDialog1.FileName;
                    DataTable table = (DataTable)MiniExcel.QueryAsDataTable(path, useHeaderRow: true);
                    if (table.Rows.Count > 0)
                    {
                        if (FormGuid == "")
                        {
                            FormGuid = Guid.NewGuid().ToString();
                        }
                        for (int i = 0; i < table.Rows.Count; i++)
                        {
                            string VolumeCodeNew = table.Rows[i]["热轧批号"].ToString();
                            string outPutSize = table.Rows[i]["轧制规格"].ToString();
                            string nextWorkType = table.Rows[i]["去向"].ToString();
                            string productSize = table.Rows[i]["成品规格"].ToString();
                            string remark = table.Rows[i]["备注"].ToString();
                            InsertTemp计划(FormGuid, VolumeCodeNew, nextWorkType, outPutSize, productSize, remark);
                        }
                        ListTempPlan();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("添加失败了!" + ex.ToString(), "提示", MessageBoxButtons.OK);
                return;
            }
        }



        private void uiComboBox产线_SelectedIndexChanged(object sender, EventArgs e)
        {
            string workLineCode;
            if (this.uiComboBox产线.SelectedValue.ToString() != "")
            {
                workLineCode = this.uiComboBox产线.SelectedValue.ToString();
                Dic_WorkLine dic_workline = pub.GetWorkLineByLineCode(工厂编号1, workLineCode);
                if (dic_workline.DepartCode != null)
                {
                    Dic_Depart dic_depart = pub.GetDepartByCode(dic_workline.DepartCode, 工厂编号1);
                    if (dic_depart.DepartCode != "")
                    {
                        this.uiTextBox部门1.Text = dic_depart.DepartCode;
                        this.uiTextBox部门名称1.Text = dic_depart.DepartName;
                    }
                    else
                    {
                        this.uiTextBox部门1.Text = "";
                        this.uiTextBox部门名称1.Text = "";
                    }
                }
                else
                {
                    this.uiTextBox部门1.Text = "";
                    this.uiTextBox部门名称1.Text = "";
                }


                pub.ListWorkType(工厂编号1, "硅钢", this.uiComboBox去向);
                //pub.ListWorkTeam(工厂编号1, workLineCode, this.uiComboBoxWorkTeam);
            }
        }
    }
}
